package com.shahe.GirlsDay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.shahe.GirlsDay.MyView.CalendarView;
import com.shahe.GirlsDay.util.StaticClass;

import java.util.Date;

public class CalendarActivity extends Activity {
    /**
     * Called when the activity is first created.
     */


    private TextView calendarCenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendarpage);

        //èŽ·å�–æ—¥åŽ†æŽ§ä»¶å¯¹è±¡
        final CalendarView calendar = (CalendarView)findViewById(R.id.calendarView);
//èŽ·å�–æ—¥åŽ†ä¸­å¹´æœˆ ya[0]ä¸ºå¹´ï¼Œya[1]ä¸ºæœˆï¼ˆæ ¼å¼�å¤§å®¶å�¯ä»¥è‡ªè¡Œåœ¨æ—¥åŽ†æŽ§ä»¶ä¸­æ”¹ï¼‰
        String[] ya = calendar.getYearAndmonth().split("-");
//ç‚¹å‡»ä¸Šä¸€æœˆ å�Œæ ·è¿”å›žå¹´æœˆ
        String leftYearAndmonth = calendar.clickLeftMonth();
        String[] lya = leftYearAndmonth.split("-");

//ç‚¹å‡»ä¸‹ä¸€æœˆ
        String rightYearAndmonth = calendar.clickRightMonth();
        String[] rya = rightYearAndmonth.split("-");

//è®¾ç½®æŽ§ä»¶ç›‘å�¬ï¼Œå�¯ä»¥ç›‘å�¬åˆ°ç‚¹å‡»çš„æ¯�ä¸€å¤©ï¼ˆå¤§å®¶ä¹Ÿå�¯ä»¥åœ¨æŽ§ä»¶ä¸­è‡ªè¡Œè®¾å®šï¼‰
        calendar.setOnItemClickListener(new  calendarItemClickListener());

        final ImageButton leftBtn = (ImageButton)findViewById(R.id.calendarLeft);
        final ImageButton rightBtn = (ImageButton)findViewById(R.id.calendarRight);
        final ImageButton logBtn = (ImageButton)findViewById(R.id.btn_cal_add);



        calendarCenter = (TextView)findViewById(R.id.calendarCenter);
        calendarCenter.setText(calendar.getYearAndmonth());

        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                calendarCenter.setText(calendar.clickLeftMonth());
                //Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
                //new date() = now time and date
                //Toast.makeText(getApplicationContext(), new Date().toString(), Toast.LENGTH_SHORT).show();
            }
        });

        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendarCenter.setText(calendar.clickRightMonth());
                // Toast.makeText(getApplicationContext(), rightYearAndmonth, Toast.LENGTH_SHORT).show();
            }
        });

        logBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	
            	//StaticClass.currentDate;
            	
                Intent intent = new Intent(CalendarActivity.this,  LogActivity.class);
                startActivity(intent);
            }
        });


        final Button homeButton = (Button) findViewById(R.id.btn_home);

        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        

    }

    class calendarItemClickListener implements CalendarView.OnItemClickListener{
        @Override
        public void OnItemClick(Date date) {
            
        	
        	
        	//changing date
        	String[] day = date.toString().split(" ");        	
        	StaticClass.currentDate = day[5] + "-" + String.valueOf(date.getMonth()+1) + "-" + day[2];
        	
        	Toast.makeText(getApplicationContext(), StaticClass.currentDate, Toast.LENGTH_SHORT).show();
        }
    }
}
